package com.example.newstudent.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.newstudent.dao.lessonDao;
import com.example.newstudent.dao.optional_courseDao;
import com.example.newstudent.dao.studentDao;
import com.example.newstudent.entity.lesson;
import com.example.newstudent.entity.optional_course;
import com.example.newstudent.entity.student;
import com.example.newstudent.service.studentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class studentImpl implements studentService {
    @Autowired
    studentDao studentDao;
    @Autowired
    lessonDao lessonDao;
    @Autowired
    optional_courseDao optional_courseDao;

    @Override
    public student login(student student) {
        QueryWrapper<student> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",student.getId()).eq("pin",student.getPin());
        return studentDao.selectOne(queryWrapper);
    }

    @Override
    public List<lesson> getlesson(String id,int page) {
        QueryWrapper<lesson> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("studentid",id).eq("weeks",page);
        return lessonDao.selectList(queryWrapper);
    }

    @Override
    public void status(String id) {
        UpdateWrapper<student> updateWrapper = new UpdateWrapper<>();
        LocalDateTime localDateTime = LocalDateTime.now();
        updateWrapper.eq("id",id).set("status",1).set("time",localDateTime);
        studentDao.update(null,updateWrapper);
    }

    @Override
    public void selectLesson(String id, String name) {
        QueryWrapper<optional_course> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",name);
        List<optional_course> optional_courses = optional_courseDao.selectList(queryWrapper);
        for (int i = 0; i < optional_courses.size(); i++) {
            lesson lesson = new lesson();
            lesson.setAll(optional_courses.get(i),id);
            lessonDao.insert(lesson);
        }
    }

    @Override
    public student updatemessage(student student) {
        UpdateWrapper<student> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",student.getId());
        studentDao.update(student,updateWrapper);
        QueryWrapper<student> queryWrapper = new QueryWrapper<>();
        return studentDao.selectOne(queryWrapper.eq("id",student.getId()));
    }

    @Override
    public int enroll(student student) {
        QueryWrapper<student> studentQueryWrapper = new QueryWrapper<>();
        studentQueryWrapper.eq("id",student.getId());
        if (studentDao.selectOne(studentQueryWrapper) != null) {
            return 0 ;
        }else{
        studentDao.insert(student);
        return 1 ;
        }
    }

    @Override
    public List<lesson> haveSelect(String id) {
        QueryWrapper<lesson> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",2).eq("studentid",id);
        return lessonDao.selectList(queryWrapper);
    }

    @Override
    public void withDrawal(String id, String name) {
        QueryWrapper<lesson> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("studentid",id).eq("name",name);
        lessonDao.delete(queryWrapper);
    }

    @Override
    public int getStudentHaveStatus() {
        QueryWrapper<student> studentQueryWrapper = new QueryWrapper<>();
        studentQueryWrapper.eq("status",1);
        return studentDao.selectCount(studentQueryWrapper);
    }

    @Override
    public List<Double> gender() {
        List<Double> doubles = new ArrayList<>();
        QueryWrapper<student> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("gender","男");
        double man = studentDao.selectCount(queryWrapper);
        doubles.add(man);
        QueryWrapper<student> studentQueryWrapper =new QueryWrapper<>();
        studentQueryWrapper.eq("gender","女");
        double woman = studentDao.selectCount(studentQueryWrapper);
        doubles.add(woman);
        doubles.add(man/(woman+man)*100);
        doubles.add(woman/(woman+man)*100);
        return doubles;
    }

    @Override
    public List<Integer> provinces() {
        String province = null;
        List<Integer> integers = new ArrayList<>();
        for (int i = 1; i < 14; i++) {
            switch (i){
                case  1:province="重庆";break;
                case  2:province="四川";break;
                case  3:province="贵州";break;
                case  4:province="山西";break;
                case  5:province="河北";break;
                case  6:province="河南";break;
                case  7:province="陕西";break;
                case  8:province="西藏";break;
                case  9:province="新疆";break;
                case  10:province="北京";break;
                case  11:province="黑龙江";break;
                case  12:province="广州";break;
                case  13:province="海南";break;
            }
            QueryWrapper<student> queryWrapper  = new QueryWrapper<>();
            queryWrapper.eq("provinces", province);
            integers.add(studentDao.selectCount(queryWrapper));
        }
        return integers;
    }

    @Override
    public List<LocalDateTime> haveStatus() {
        return studentDao.selectByTime();
    }

    @Override
    public List<String> academy() {
        return studentDao.academy();
    }

    @Override
    public student selectById(String id) {
        return studentDao.selectById(id);
    }

}
